package com.training.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Menu;
import com.training.common.model.Power;
import com.training.service.MenuService;
import com.training.service.MenuServiceImp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MenuController extends Controller {

    @Inject(MenuServiceImp.class)
    private MenuService menuService;

    public void index(){
        render("/views/menu/index.html");
    }

    public void getMenuList(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String menuName = getPara("menuName");
        Page<Menu> menuList = menuService.getMenuPage(pageNumber,pageSize,menuName);
        Result result = new Result();
        result.setData(new TrainingPage(menuList));
        renderJson(result);
    }

    public void getMenutree(){
        String roleid = getPara("roleid");
        List<Menu> menuTree = menuService.getMenuTree();
        List<Menu> menuTreetrue = menuService.getMenuTreetrue(roleid);
        Map<String,Object> resultmap = new HashMap<String,Object>();
        resultmap.put("code",0);
        List<Menu> rendermenutree = new ArrayList<Menu>();
        for (Menu menu : menuTree){
            if (menu.getParentMenuId().equals("0")){
                int menuid = menu.getMenuId();
                List<Menu> menuList1 = new ArrayList<Menu>();
                for (Menu menu1 : menuTree){
                    if (menuid == Integer.parseInt(menu1.getParentMenuId()) && !menu1.getParentMenuId().equals("0")){
                        List<Map<String,Object>> powerlistchildren = new ArrayList<Map<String, Object> > ();
                        List<Power> powerList = Power.dao.getPowerList(menu1.getMenuId().toString());
                        Map<String,Object> powerMap = null;
                        for(Power power : powerList){
                            powerMap = new HashMap<String,Object>();
                            powerMap.put("parentMenuId",menu1.getMenuId());
                            powerMap.put("menuId",menu1.getMenuId()+"-"+power.getId());
                            powerMap.put("menuName",power.getName());

                            boolean boo = Power.dao.isPowerRole(roleid,power.getId().toString());
                            if(boo){
                                powerMap.put("checked","true");
                            }
                            powerlistchildren.add(powerMap);
                        }
                        menu1.put("children",powerlistchildren);
                        menuList1.add(menu1);
                    }
                }
                menu.put("children",menuList1);
                rendermenutree.add(menu);
            }
        }


        resultmap.put("data",rendermenutree);
        renderJson(resultmap);
    }
}
